using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UIDesigner.ControlProviders.Bases;
using UIDesigner.FieldHandlers;
using System.ComponentModel;
using System.Drawing;
using DDW;
using System.Diagnostics;
using System.Windows.Forms;
using AFSharedLibrary.SharedControls;
using SharedClasses;
using UIDesigner.FieldHandlers.Bases;

namespace UIDesigner.ControlProviders
{
    [ControlProvider(ContainerControlOptions.DefaultProgramLink, GridColumnOptions.DefaultProgramLinkColumn, ListViewColumnOptions.DefaultProgramLinkColumn)]
	public class DefaultProgramLauncherProvider : BaseControlProvider
    {
        public DefaultProgramLauncherProvider(AFDesignerHost objHost) : base(objHost)
        {
        }

        public override void AddBindings(Control ctrlBinding, BaseNode objBase, string strBindingSourceProperty)
        {
            LinkLabel ctrlLinkLabel = ctrlBinding as LinkLabel;
            BindingSource objBindingSource = GetBindingSource(strBindingSourceProperty);

            ctrlLinkLabel.DataBindings.Clear();
            ctrlLinkLabel.DataBindings.Add(new System.Windows.Forms.Binding("Link", objBindingSource, objBase.Name, true));
        }

        public override Control AddControls(string strOption, TableLayoutPanel ctrlParent, BaseNode objBase, bool bDefault, bool bPreview, int nTabIndex, List<TableLayoutPanelCellPosition> rgTableLayoutPanelCellPositions, List<char> rgMnemonics)
        {
            string strName;
            string strPrettyName;
            string strPrettyDescription;
            LinkLabel ctrlLinkLabel;

            Debug.Assert(this.SupportsOption(strOption));

            strName = HandleName(objBase);
            strName = HandleName(GetControlPrefix(typeof(LinkLabel)) + strName);

            if (bPreview)
            {
                ctrlLinkLabel = new LinkLabel();
            }
            else
            {
                ctrlLinkLabel = (LinkLabel) m_objHost.CreateComponent(typeof(LinkLabel), strName);
            }

            strPrettyName = GetPrettyName(objBase);
            strPrettyDescription = GetPrettyDescription(objBase);

            ctrlLinkLabel.Name = strName;
            ctrlLinkLabel.TabIndex = nTabIndex;

            ctrlLinkLabel.AccessibleName = strPrettyName;
            ctrlLinkLabel.AccessibleDescription = strPrettyDescription;

            ctrlParent.Controls.Add(ctrlLinkLabel, rgTableLayoutPanelCellPositions[0].Column, rgTableLayoutPanelCellPositions[0].Row);

            return ctrlLinkLabel;
        }

        public override Control AddControls(string strOption, IComponent ctrlParent, BaseNode objBase, bool bDefault, bool bPreview, int nTabIndex, List<Rectangle> rgLocationRects, List<char> rgMnemonics)
        {
            string strName;
            string strPrettyName;
            string strPrettyDescription;

            Debug.Assert(this.SupportsOption(strOption));

            if (ctrlParent is DataGrid)
            {

            }
            else if (ctrlParent is ListView)
            {

            }
            else if (ctrlParent is TreeView)
            {

            }
            else if (ctrlParent is Control)
            {
                LinkLabel ctrlLinkLabel;
                Control ctrlParentControl = (Control) ctrlParent; 

                strName = HandleName(objBase);
                strName = HandleName(GetControlPrefix(typeof(LinkLabel)) + strName);

                if (bPreview)
                {
                    ctrlLinkLabel = new LinkLabel();
                }
                else
                {
                    ctrlLinkLabel = (LinkLabel)m_objHost.CreateComponent(typeof(LinkLabel), strName);
                }

                strPrettyName = GetPrettyName(objBase);
                strPrettyDescription = GetPrettyDescription(objBase);

                ctrlLinkLabel.Location = new System.Drawing.Point(rgLocationRects[1].Left, rgLocationRects[1].Top);
                ctrlLinkLabel.Name = strName;
                ctrlLinkLabel.Tag = strName;
                ctrlLinkLabel.TabIndex = nTabIndex;

                ctrlLinkLabel.AccessibleName = strPrettyName;
                ctrlLinkLabel.AccessibleDescription = strPrettyDescription;

                ctrlParentControl.Controls.Add(ctrlLinkLabel);

                return ctrlLinkLabel;
            }

            return null;
        }

        public override void NegotiateLayout(string strOption, IComponent ctrlParent, BaseNode objBase, LayoutMode eLayoutMode, ref int nTabIndex, out LayoutStyle eLayoutStyle, out int nLabelAlignment, out Size szControlSize)
        {

            Debug.Assert(this.SupportsOption(strOption));

            eLayoutStyle = LayoutStyle.Unassigned;
            nLabelAlignment = -1;
            szControlSize = new Size();

            if (ctrlParent is DataGrid)
            {

            }
            else if (ctrlParent is ListView)
            {

            }
            else if (ctrlParent is TreeView)
            {

            }
            else if (ctrlParent is Control)
            {
                Label ctrlLabel = new Label();
                string strPrettyName = GetPrettyName(objBase);
                WindowsAPI.SIZEAPI sz = new WindowsAPI.SIZEAPI();
                int hwnd = ctrlLabel.Handle.ToInt32();
                int hdc = WindowsAPI.GetDC(hwnd);

                ctrlLabel.Margin = new Padding(ctrlLabel.Margin.Left, ctrlLabel.Margin.Top + 3, ctrlLabel.Margin.Right, ctrlLabel.Margin.Bottom);

                WindowsAPI.GetTextExtentPoint32(hdc, ref strPrettyName, strPrettyName.Length, ref sz);
                WindowsAPI.ReleaseDC(hwnd, hdc); 

                eLayoutStyle = LayoutStyle.SingleCell;
                nLabelAlignment = sz.cy + ctrlLabel.Margin.Top + ctrlLabel.Margin.Bottom;

                szControlSize.Width = 100;
                szControlSize.Height = 24;

                nTabIndex += 1;
            }
        }
    }
}
